

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>mindspore.common.initializer &mdash; MindSpore master documentation</title>
  

  
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
        <script src="../_static/jquery.js"></script>
        <script src="../_static/underscore.js"></script>
        <script src="../_static/doctools.js"></script>
        <script src="../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="mindspore.communication" href="mindspore.communication.html" />
    <link rel="prev" title="mindspore.ms_memory_recycle" href="mindspore/mindspore.ms_memory_recycle.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../index.html" class="icon icon-home"> MindSpore
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">MindSpore Python API</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="mindspore.html">mindspore</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">mindspore.common.initializer</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.communication.html">mindspore.communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.compression.html">mindspore.compression</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.context.html">mindspore.context</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.dataset.html">mindspore.dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.dataset.audio.html">mindspore.dataset.audio</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.dataset.config.html">mindspore.dataset.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.dataset.text.html">mindspore.dataset.text</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.dataset.transforms.html">mindspore.dataset.transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.dataset.vision.html">mindspore.dataset.vision</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.mindrecord.html">mindspore.mindrecord</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.nn.html">mindspore.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.nn.probability.html">mindspore.nn.probability</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.nn.transformer.html">mindspore.nn.transformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.numpy.html">mindspore.numpy</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.ops.html">mindspore.ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.parallel.html">mindspore.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.parallel.nn.html">mindspore.parallel.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.profiler.html">mindspore.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.scipy.html">mindspore.scipy</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.train.html">mindspore.train</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.boost.html">mindspore.boost</a></li>
</ul>
<p class="caption"><span class="caption-text">MindSpore C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://www.mindspore.cn/lite/api/zh-CN/master/api_cpp/mindspore.html">MindSpore Lite↗</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">MindSpore</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
        
      <li>mindspore.common.initializer</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
          
            <a href="../_sources/api_python/mindspore.common.initializer.rst.txt" rel="nofollow"> View page source</a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="mindspore-common-initializer">
<h1>mindspore.common.initializer<a class="headerlink" href="#mindspore-common-initializer" title="Permalink to this headline">¶</a></h1>
<p>初始化神经元参数。</p>
<dl class="class">
<dt id="mindspore.common.initializer.Initializer">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.common.initializer.</code><code class="sig-name descname">Initializer</code><span class="sig-paren">(</span><em class="sig-param">**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.common.initializer.Initializer" title="Permalink to this definition">¶</a></dt>
<dd><p>初始化器的抽象基类。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>kwargs</strong> (dict) – <cite>Initializer</cite> 的关键字参数。</p></li>
</ul>
</dd></dl>

<dl class="class">
<dt id="mindspore.common.initializer.TruncatedNormal">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.common.initializer.</code><code class="sig-name descname">TruncatedNormal</code><span class="sig-paren">(</span><em class="sig-param">sigma=0.01</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.common.initializer.TruncatedNormal" title="Permalink to this definition">¶</a></dt>
<dd><p>生成一个服从截断正态（高斯）分布的随机数组用于初始化Tensor。</p>
<p><strong>参数：</strong></p>
<p><strong>sigma</strong> (float) - 截断正态分布的标准差，默认值为0.01。</p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.common.initializer</span> <span class="kn">import</span> <span class="n">initializer</span><span class="p">,</span> <span class="n">TruncatedNormal</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor1</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">TruncatedNormal</span><span class="p">(),</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor2</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="s1">&#39;truncatedNormal&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="mindspore.common.initializer.Normal">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.common.initializer.</code><code class="sig-name descname">Normal</code><span class="sig-paren">(</span><em class="sig-param">sigma=0.01</em>, <em class="sig-param">mean=0.0</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.common.initializer.Normal" title="Permalink to this definition">¶</a></dt>
<dd><p>生成一个服从正态分布N(sigma, mean)的随机数组用于初始化Tensor。</p>
<div class="math notranslate nohighlight">
\[f(x) =  \frac{1} {\sqrt{2*π} * sigma}exp(-\frac{(x - mean)^2} {2*{sigma}^2})\]</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>sigma</strong> (float) - 正态分布的标准差，默认值为0.01。</p></li>
<li><p><strong>mean</strong> (float) - 正态分布的均值，默认值为0.0。</p></li>
</ul>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.common.initializer</span> <span class="kn">import</span> <span class="n">initializer</span><span class="p">,</span> <span class="n">Normal</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor1</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">Normal</span><span class="p">(),</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor2</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="s1">&#39;normal&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="mindspore.common.initializer.Uniform">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.common.initializer.</code><code class="sig-name descname">Uniform</code><span class="sig-paren">(</span><em class="sig-param">scale=0.07</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.common.initializer.Uniform" title="Permalink to this definition">¶</a></dt>
<dd><p>生成一个服从均匀分布U(-scale, scale)的随机数组用于初始化Tensor。</p>
<p><strong>参数：</strong></p>
<p><strong>scale</strong> (float) - 均匀分布的边界，默认值为0.07。</p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.common.initializer</span> <span class="kn">import</span> <span class="n">initializer</span><span class="p">,</span> <span class="n">Uniform</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor1</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">Uniform</span><span class="p">(),</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor2</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="s1">&#39;uniform&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="mindspore.common.initializer.HeUniform">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.common.initializer.</code><code class="sig-name descname">HeUniform</code><span class="sig-paren">(</span><em class="sig-param">negative_slope=0</em>, <em class="sig-param">mode=&quot;fan_in&quot;</em>, <em class="sig-param">nonlinearity=&quot;leaky_relu&quot;</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.common.initializer.HeUniform" title="Permalink to this definition">¶</a></dt>
<dd><p>生成一个服从HeKaiming均匀分布U(-boundary, boundary)的随机数组用于初始化Tensor，其中：</p>
<div class="math notranslate nohighlight">
\[boundary = \text{gain} \times \sqrt{\frac{3}{fan\_mode}}\]</div>
<p>其中，gain是一个可选的缩放因子。fan_mode是权重Tensor中输入或输出单元的数量，取决于mode是”fan_in”或是”fan_out”。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>negative_slope</strong> (int, float, bool) - 本层激活函数的负数区间斜率（仅适用于非线性激活函数”leaky_relu”），默认值为0。</p></li>
<li><p><strong>mode</strong> (str) - 可选”fan_in”或”fan_out”，”fan_in”会保留前向传递中权重方差的量级，”fan_out”会保留反向传递的量级，默认为”fan_in”。</p></li>
<li><p><strong>nonlinearity</strong> (str) - 非线性激活函数，推荐使用”relu”或”leaky_relu”，默认为”leaky_relu”。</p></li>
</ul>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.common.initializer</span> <span class="kn">import</span> <span class="n">initializer</span><span class="p">,</span> <span class="n">HeUniform</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor1</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">HeUniform</span><span class="p">(),</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor2</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="s1">&#39;he_uniform&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="mindspore.common.initializer.HeNormal">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.common.initializer.</code><code class="sig-name descname">HeNormal</code><span class="sig-paren">(</span><em class="sig-param">negative_slope=0</em>, <em class="sig-param">mode=&quot;fan_in&quot;</em>, <em class="sig-param">nonlinearity=&quot;leaky_relu&quot;</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.common.initializer.HeNormal" title="Permalink to this definition">¶</a></dt>
<dd><p>生成一个服从HeKaiming正态分布N(0, sigma^2)的随机数组用于初始化Tensor，其中：</p>
<div class="math notranslate nohighlight">
\[sigma = \frac{gain} {\sqrt{fan\_mode}}\]</div>
<p>其中，gain是一个可选的缩放因子。如果mode是”fan_in”，则fan_mode是权重Tensor中输入单元的数量，如果mode是”fan_out”，
fan_mode是权重Tensor中输出单元的数量。</p>
<p>HeUniform 算法的详细信息，请查看 <a class="reference external" href="https://arxiv.org/abs/1502.01852">https://arxiv.org/abs/1502.01852</a>。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>negative_slope</strong> (int, float, bool) - 本层激活函数的负数区间斜率（仅适用于非线性激活函数”leaky_relu”），默认值为0。</p></li>
<li><p><strong>mode</strong> (str) - 可选”fan_in”或”fan_out”，”fan_in”会保留前向传递中权重方差的量级，”fan_out”会保留反向传递的量级，默认为”fan_in”。</p></li>
<li><p><strong>nonlinearity</strong> (str) - 非线性激活函数，推荐使用”relu”或”leaky_relu”，默认为”leaky_relu”。</p></li>
</ul>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.common.initializer</span> <span class="kn">import</span> <span class="n">initializer</span><span class="p">,</span> <span class="n">HeNormal</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor1</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">HeNormal</span><span class="p">(),</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor2</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="s1">&#39;he_normal&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="mindspore.common.initializer.XavierUniform">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.common.initializer.</code><code class="sig-name descname">XavierUniform</code><span class="sig-paren">(</span><em class="sig-param">gain=1</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.common.initializer.XavierUniform" title="Permalink to this definition">¶</a></dt>
<dd><p>生成一个服从Xarvier均匀分布U(-boundary, boundary)的随机数组用于初始化Tensor，均匀分布的取值范围为[-boundary, boundary]，其中：</p>
<div class="math notranslate nohighlight">
\[boundary = gain * \sqrt{\frac{6}{n_{in} + n_{out}}}\]</div>
<p><span class="math notranslate nohighlight">\(gain\)</span> 是一个可选的缩放因子。<span class="math notranslate nohighlight">\(n_{in}\)</span> 为权重Tensor中输入单元的数量。<span class="math notranslate nohighlight">\(n_{out}\)</span> 为权重Tensor中输出单元的数量。</p>
<p>有关 XavierUniform 算法的详细信息，请查看 <a class="reference external" href="http://proceedings.mlr.press/v9/glorot10a.html">http://proceedings.mlr.press/v9/glorot10a.html</a>。</p>
<p><strong>参数：</strong></p>
<p><strong>gain</strong> (float) - 可选的缩放因子，默认值为1。</p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.common.initializer</span> <span class="kn">import</span> <span class="n">initializer</span><span class="p">,</span> <span class="n">XavierUniform</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor1</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">XavierUniform</span><span class="p">(),</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor2</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="s1">&#39;xavier_uniform&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="mindspore.common.initializer.One">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.common.initializer.</code><code class="sig-name descname">One</code><span class="sig-paren">(</span><em class="sig-param">**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.common.initializer.One" title="Permalink to this definition">¶</a></dt>
<dd><p>生成一个值全为1的常量数组用于初始化Tensor。</p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.common.initializer</span> <span class="kn">import</span> <span class="n">initializer</span><span class="p">,</span> <span class="n">One</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor1</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">One</span><span class="p">(),</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor2</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="s1">&#39;ones&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="mindspore.common.initializer.Zero">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.common.initializer.</code><code class="sig-name descname">Zero</code><span class="sig-paren">(</span><em class="sig-param">**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.common.initializer.Zero" title="Permalink to this definition">¶</a></dt>
<dd><p>生成一个值全为0的常量数组用于初始化Tensor。</p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.common.initializer</span> <span class="kn">import</span> <span class="n">initializer</span><span class="p">,</span> <span class="n">Zero</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor1</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">Zero</span><span class="p">(),</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor2</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="s1">&#39;zeros&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="mindspore.common.initializer.Constant">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.common.initializer.</code><code class="sig-name descname">Constant</code><span class="sig-paren">(</span><em class="sig-param">value</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.common.initializer.Constant" title="Permalink to this definition">¶</a></dt>
<dd><p>生成一个常量数组用于初始化Tensor。</p>
<p><strong>参数：</strong></p>
<p><strong>value</strong> (Union[int, numpy.ndarray]) - 用于初始化的常数值或者数组。</p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.common.initializer</span> <span class="kn">import</span> <span class="n">initializer</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor1</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor2</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="mindspore.common.initializer.initializer">
<code class="sig-prename descclassname">mindspore.common.initializer.</code><code class="sig-name descname">initializer</code><span class="sig-paren">(</span><em class="sig-param">init</em>, <em class="sig-param">shape=None</em>, <em class="sig-param">dtype=mstype.float32</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.common.initializer.initializer" title="Permalink to this definition">¶</a></dt>
<dd><p>创建并初始化一个Tensor。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>init</strong> (Union[Tensor, str, Initializer, numbers.Number]) – 初始化方式。</p>
<ul>
<li><p><strong>str</strong> - <cite>init</cite> 是继承自 <cite>Initializer</cite> 的类的别名，实际使用时会调用相应的类。<cite>init</cite> 的值可以是”normal”、”ones”或”zeros”等。</p></li>
<li><p><strong>Initializer</strong> - <cite>init</cite> 是继承自 <cite>Initializer</cite> ，用于初始化Tensor的类。</p></li>
<li><p><strong>numbers.Number</strong> - 用于初始化Tensor的常量。</p></li>
</ul>
</li>
<li><p><strong>shape</strong> (Union[[tuple, list, int]) - 被初始化的Tensor的shape，默认值为None。</p></li>
<li><p><strong>dtype</strong> (mindspore.dtype) – 被初始化的Tensor的数据类型，默认值为 <cite>mindspore.float32</cite> 。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Tensor。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>TypeError</strong> - 参数 <cite>init</cite> 的类型不正确。</p></li>
<li><p><strong>ValueError</strong> - 当 <cite>init</cite> 传入Tensor对象时， <cite>init</cite> 的shape与形参 <cite>shape</cite> 内的数值不一致。</p></li>
</ul>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.common.initializer</span> <span class="kn">import</span> <span class="n">initializer</span><span class="p">,</span> <span class="n">One</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor1</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="s1">&#39;ones&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor2</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">One</span><span class="p">(),</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor3</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="mindspore.common.initializer.Identity">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.common.initializer.</code><code class="sig-name descname">Identity</code><span class="sig-paren">(</span><em class="sig-param">**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.common.initializer.Identity" title="Permalink to this definition">¶</a></dt>
<dd><p>生成一个2维的单位阵用于初始化Tensor。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>ValueError</strong> - 被初始化的Tensor的维度不等于2。</p></li>
</ul>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.common.initializer</span> <span class="kn">import</span> <span class="n">initializer</span><span class="p">,</span> <span class="n">Identity</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor1</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">Identity</span><span class="p">(),</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor2</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="s1">&#39;identity&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="mindspore.common.initializer.Sparse">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.common.initializer.</code><code class="sig-name descname">Sparse</code><span class="sig-paren">(</span><em class="sig-param">sparsity</em>, <em class="sig-param">sigma=0.01</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.common.initializer.Sparse" title="Permalink to this definition">¶</a></dt>
<dd><p>生成一个2维的稀疏矩阵用于初始化Tensor。矩阵非0的位置的值服从正态分布N(0, 0.01)。</p>
<p><strong>参数：</strong></p>
<p><strong>sparsity</strong> (float) - 矩阵每列中元素被置0的比例。
<strong>sigma</strong> (float) - 正态分布的标准差，默认值为0.01。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>ValueError</strong> - 被初始化的Tensor的维度不等于2。</p></li>
</ul>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.common.initializer</span> <span class="kn">import</span> <span class="n">initializer</span><span class="p">,</span> <span class="n">Sparse</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor1</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">Sparse</span><span class="p">(</span><span class="n">sparsity</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">sigma</span><span class="o">=</span><span class="mf">0.01</span><span class="p">),</span> <span class="p">[</span><span class="mi">5</span><span class="p">,</span> <span class="mi">8</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="mindspore.common.initializer.Dirac">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.common.initializer.</code><code class="sig-name descname">Dirac</code><span class="sig-paren">(</span><em class="sig-param">group=1</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.common.initializer.Dirac" title="Permalink to this definition">¶</a></dt>
<dd><p>利用Dirac delta函数生成一个array用于初始化Tensor。这种初始化方式将会保留卷积层的输入。对于group
卷积，通道的每个分组会被分别保留。</p>
<p><strong>参数：</strong></p>
<p><strong>group</strong> (int) - 卷积层中的分组，默认值为1。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>ValueError</strong> - group不在[3, 4, 5]的范围内。</p></li>
<li><p><strong>ValueError</strong> - 初始化的Tensor的第一个维度不能被group整除。</p></li>
</ul>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.common.initializer</span> <span class="kn">import</span> <span class="n">initializer</span><span class="p">,</span> <span class="n">Dirac</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor1</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">Dirac</span><span class="p">(</span><span class="n">groups</span><span class="o">=</span><span class="mi">2</span><span class="p">),</span> <span class="p">[</span><span class="mi">6</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor2</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="s2">&quot;dirac&quot;</span><span class="p">,</span> <span class="p">[</span><span class="mi">6</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="mindspore.common.initializer.Orthogonal">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.common.initializer.</code><code class="sig-name descname">Orthogonal</code><span class="sig-paren">(</span><em class="sig-param">gain=1.</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.common.initializer.Orthogonal" title="Permalink to this definition">¶</a></dt>
<dd><p>生成一个(半)正交矩阵用于初始化Tensor。被初始化的Tensor的维度至少为2。
如果维度大于2，多余的维度将会被展平。</p>
<p><strong>参数：</strong></p>
<p><strong>gain</strong> (float) - 可选的比例因子，默认值为1。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>ValueError</strong> - 被初始化的Tensor的维度小于2。</p></li>
</ul>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.common.initializer</span> <span class="kn">import</span> <span class="n">initializer</span><span class="p">,</span> <span class="n">Orthogonal</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor1</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">Orthogonal</span><span class="p">(</span><span class="n">gain</span><span class="o">=</span><span class="mf">2.</span><span class="p">),</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor2</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="s1">&#39;orthogonal&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="mindspore.common.initializer.VarianceScaling">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.common.initializer.</code><code class="sig-name descname">VarianceScaling</code><span class="sig-paren">(</span><em class="sig-param">scale=1.0</em>, <em class="sig-param">mode=&quot;fan_in&quot;</em>, <em class="sig-param">distribution=&quot;truncated_normal&quot;</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.common.initializer.VarianceScaling" title="Permalink to this definition">¶</a></dt>
<dd><p>生成一个随机的array用于初始化Tensor。
当distribution是”truncated_normal”或者”untruncated_normal”时，array中的值将服从均值为0，标准差
为 <span class="math notranslate nohighlight">\(stddev = sqrt(scale/n)\)</span> 的截断或者非截断正太分布。如果mode是”fan_in”， <span class="math notranslate nohighlight">\(n\)</span> 是输入单元的数量；
如果mode是”fan_out”， <span class="math notranslate nohighlight">\(n\)</span> 是输出单元的数量；如果mode是”fan_avg”， <span class="math notranslate nohighlight">\(n\)</span> 是输入输出单元数量的均值。
当distribution是”uniform”时，array中的值将服从均匀分布[<cite>-sqrt(3*scale/n)</cite>, <cite>sqrt(3*scale/n)</cite>]。</p>
<p><strong>参数：</strong></p>
<p><strong>scale</strong> (float) - 比例因子，默认值为1.0。
<strong>mode</strong> (str) - 其值应为”fan_in”，”fan_out”或者”fan_avg”，默认值为”fan_in”。
<strong>distribution</strong> (str) - 用于采样的分布类型。它可以是”uniform”，”truncated_normal”或”untruncated_normal”，
默认值为”truncated_normal”。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>ValueError</strong> - scale小于等于0。</p></li>
<li><p><strong>ValueError</strong> - mode不是”fan_in”，”fan_out”或者”fan_avg”。</p></li>
<li><p><strong>ValueError</strong> - distribution不是”truncated_normal”，”untruncated_normal”或者”uniform”。</p></li>
</ul>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.common.initializer</span> <span class="kn">import</span> <span class="n">initializer</span><span class="p">,</span> <span class="n">VarianceScaling</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor1</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">VarianceScaling</span><span class="p">(</span><span class="n">scale</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;fan_out&#39;</span><span class="p">,</span>
<span class="gp">... </span>                                      <span class="n">distribution</span><span class="o">=</span><span class="s1">&#39;untruncated_normal&#39;</span><span class="p">),</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tensor2</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="s1">&#39;varianceScaling&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

</div>


           </div>
           
          </div>
          <footer>
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
        <a href="mindspore.communication.html" class="btn btn-neutral float-right" title="mindspore.communication" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
        <a href="mindspore/mindspore.ms_memory_recycle.html" class="btn btn-neutral float-left" title="mindspore.ms_memory_recycle" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, MindSpore.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>